package com.open.party.cloud.view.home.jiCengDangJian.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.open.party.cloud.R;
import com.open.party.cloud.model.PartyDevFileBean;
import com.sinothk.android.utils.XUtils;
import com.sinothk.android.utils.inters.OnSuperViewListener;

import java.util.ArrayList;
import java.util.List;


public class DyfzDevFileListAdapter extends RecyclerView.Adapter<DyfzDevFileListAdapter.ViewHolder> {

    // 展示数据
    private ArrayList<PartyDevFileBean> mData;
    private Context mContext;
    private OnSuperViewListener<PartyDevFileBean> onSuperListener;

    public DyfzDevFileListAdapter(Context mContext) {
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

    public void setOnSuperViewListener(OnSuperViewListener<PartyDevFileBean> onSuperListener) {
        this.onSuperListener = onSuperListener;
    }

    public void setData(List<PartyDevFileBean> data) {
        if (mData == null) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public void updateData(List<PartyDevFileBean> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public DyfzDevFileListAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.dyfz_dev_file_list_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        // 绑定数据
        final PartyDevFileBean itemData = mData.get(position);

        holder.titleTv.setText(XUtils.string().getNotNullValue(itemData.getFileName()));
        holder.subTv.setText(XUtils.string().getNotNullValue(itemData.getRemark()));

        if (onSuperListener != null) {
            holder.operation1Btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onSuperListener.onClick(position, itemData, "operation1Btn", holder.operation1Btn);
                }
            });

            holder.operation2Btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onSuperListener.onClick(position, itemData, "operation2Btn", holder.operation2Btn);
                }
            });
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView titleTv, subTv, operation1Btn, operation2Btn;

        ViewHolder(View itemView) {
            super(itemView);

            titleTv = itemView.findViewById(R.id.titleTv);
            subTv = itemView.findViewById(R.id.subTv);

            operation1Btn = itemView.findViewById(R.id.operation1Btn);
            operation2Btn = itemView.findViewById(R.id.operation2Btn);
        }
    }
}